/*
1_ParseCSLs.do

"Human Capital and Climate Change", 
	by Noam Angrist, Kevin Winseck, Harry Patrinos, and Joshua Graff Zivin.
	
	
Inputs:
	ECA-compulsoryschoolinglaws_2022-05-06.xlsx
Outputs:
	minEduPanel.dta 


	
*/
**** Reads in World Bank spreadsheet on compulsory schooling laws**********

import excel "ECA-compulsoryschoolinglaws_2022-05-06.xlsx", clear firstrow

keep Countryregion ReformImplemented Minschoolinglevelprior Minschoolinglevelafter Schoolstartingage Firstcohortpotentiallyaffecte id
rename Firstcohortpotentiallyaffecte firstAffectedCohort
rename ReformImplemented implementedYear

* Match 2 digit and 3 digit country codes
gen cntry = ""
replace cntry = "AL" if id=="ALB"
replace cntry = "AD" if id=="AND"
*replace cntry = "" if id=="ARM"
replace cntry = "AT" if id=="AUT"
*replace cntry = "" if id=="AZE"
replace cntry = "BE" if id=="BEL"
replace cntry = "BG" if id=="BGR"
replace cntry = "BA" if id=="BIH"
replace cntry = "CH" if id=="CHE"
replace cntry = "CY" if id=="CYP"
replace cntry = "CZ" if id=="CZE"
replace cntry = "DE" if id=="DEU"
replace cntry = "DK" if id=="DNK"
replace cntry = "EE" if id=="EST"
replace cntry = "ES" if id=="ESP"
replace cntry = "FI" if id=="FIN"
replace cntry = "FR" if id=="FRA"
* exclude GB since it is by region
*replace cntry = "" if id=="GEO"
replace cntry = "GR" if id=="GRC" 
replace cntry = "HU" if id=="HUN"
replace cntry = "IE" if id=="IRL"
replace cntry = "IS" if id=="ISL"
replace cntry = "IT" if id=="ITA"
*replace cntry = "" if id=="KAZ"
*replace cntry = "" if id=="KGZ"
replace cntry = "LT" if id=="LTU"
replace cntry = "LU" if id=="LUX"
replace cntry = "LV" if id=="LVA"
replace cntry = "MC" if id=="MCO"
replace cntry = "MD" if id=="MDA"
replace cntry = "MK" if id=="MKD"
replace cntry = "NL" if id=="NLD"
replace cntry = "NO" if id=="NOR"
replace cntry = "PL" if id=="POL"
replace cntry = "PT" if id=="PRT"
replace cntry = "RU" if id=="RUS"
replace cntry = "RS" if id=="SRB"
replace cntry = "SK" if id=="SVK"
replace cntry = "SE" if id=="SWE"
*replace cntry = "" if id=="TKM"
replace cntry = "TR" if id=="TUR"
replace cntry = "UA" if id=="UKR"
*replace cntry = "" if id=="UZB"
*replace cntry = "SI" if id=="" /*we don't have slovenia in WB ECA*/


drop if mi(cntry)
drop id

replace Schoolstartingage = 6 if mi(Schoolstartingage) /* Assumed school starting age if not specified*/
replace firstAffectedCohort = implementedYear - Minschoolinglevelprior - Schoolstartingage if mi(firstAffectedCohort) /* Calculated first affected cohort if not specified*/

encode cntry, gen(cntry_numeric)
xtset cntry_numeric firstAffectedCohort


tsfill, full /* Fill the panel so we get a minimum schooling level for each potential birth year */

* Fill out the panel with the previous year's values if they didn't change this year (since tsfill sets these to missing)
su firstAffectedCohort
local obs = r(N)
forval thisobs=2/`obs'{
	foreach thisvar in "Minschoolinglevelafter" "Minschoolinglevelprior" /*"Schoolstartingage"*/ /*"firstAffectedCohort"*/ "cntry"{
		replace `thisvar' = `thisvar'[`thisobs' - 1] if mi(`thisvar'[`thisobs']) & _n==`thisobs' & cntry_numeric==cntry_numeric[`thisobs'-1]
	}
	
	
}


su firstAffectedCohort
local obs = r(N) -1
* Now go through and fill out years before the first reform
forval thisobs=`obs'(-1)1{
	foreach thisvar in /*"Minschoolinglevelafter"*/ "Minschoolinglevelprior" /*"Schoolstartingage" "firstAffectedCohort"*/ "cntry"{
		replace `thisvar' = `thisvar'[`thisobs' + 1] if mi(`thisvar'[`thisobs']) & _n==`thisobs' & cntry_numeric==cntry_numeric[`thisobs'+1]
	}
	replace Minschoolinglevelafter = Minschoolinglevelprior[`thisobs'+1] if mi(Minschoolinglevelafter[`thisobs']) & _n==`thisobs' & cntry_numeric==cntry_numeric[`thisobs'+1] /*take the value from the prior min schooling for the earliest reform for everything before it */
	
}

rename Minschoolinglevelafter minEdu
rename firstAffectedCohort yrbrn /* This is the merge var*/
keep cntry minEdu yrbrn implementedYear
save "minEduPanel.dta", replace

